package net.mikoo.seals.manage.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.biz.service.InstallServices;
import net.mikoo.seals.common.WebContext;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.manage.web.common.BaseController;
import net.mikoo.seals.model.Install;
import net.mikoo.seals.model.InstallQuery;



@Controller
@RequestMapping("/install")
public class InstallController extends BaseController {
	
	Logger logger = LoggerFactory.getLogger(InstallController.class);
	
	@Autowired
	private InstallServices installServices;
	
	
	@RequestMapping("/apply")
	@ResponseBody
	public Map<String,Object> apply(Install install, HttpServletRequest request, Model model) {
		
		try {
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			Long partyId=  getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			install.setCompanyId(companyId);
			install.setPartyId(partyId);
			installServices.apply(install);
			return result(SUCCESS, "添加成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
		
	}
	
	@RequestMapping("/verify")
	@ResponseBody
	public Map<String,Object> verify(Install install, HttpServletRequest request, Model model) {
		
		try {
			
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			Long partyId=  getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			install.setCompanyId(companyId);
			install.setPartyId(partyId);
			installServices.verify(install);
			return result(SUCCESS, "添加成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
		
		
	}
	
	
	
	
	@RequestMapping("/list")
	public String list(InstallQuery query, HttpServletRequest request, Model model) {
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		
		PageModel<Install> pm  = installServices.getPageByQuery(query);
		model.addAttribute("pm", pm);
		model.addAttribute("query", query);
		return "install/list";
	}
	

	@RequestMapping("/auditing")
	public String auditing() {
		
		return  "install/auditing";
	}
	

	@RequestMapping("/edit")
	public String edit(Long installId, Model model) {
		Install install = installServices.getInfoById(installId);
		model.addAttribute("install", install);
		return "install/edit";
	}
	
	
	
	
	

}